﻿/**
系统字典下拉框数据显示
dataType:数据类型编码
controlId:控件ID
defaultValue:默认值
multiSelect:是否多选
**/
function DictionaryDropDown(dataType, controlId, defaultValue, multiSelect) {
    $.ajax({
        url: "../System/getDictionaryInfo",
        data: { type: dataType },
        dataType: "json",
        type: "POST",
        async: false,
        success: function (data) {
            if (data.result) {
                var datas = data.data;
                for (var i = 0; i < datas.length; i++) {
                    $("#" + controlId).append("<option value=\"" + datas[i]["code"] + "\" >" + datas[i]["name"]
                        + "(" + datas[i]["code"]+")"+ "</option>");
                }
                if (multiSelect != undefined && multiSelect) {
                    // moreSelect();
                } else {
                    if (defaultValue != undefined && defaultValue != "") {
                        $("#" + controlId + " option[value='" + defaultValue + "'] ").attr("selected", true);
                    }
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                }

            }
        }
    });
}

/**
部门信息下拉框
**/
function LoadDept(fileName, defaultValue) {

    $.ajax({
        url: "../System/GetDeptInfo",
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (rsl.result) {
                var reData = rsl.data;
                for (var i = 0; i < reData.length; i++) {
                    $("#" + fileName).append("<option value='" + reData[i].trCode + "'>" + reData[i].trName + "(" + reData[i].trCode + ")" + "</option>");
                }
            }
            if (defaultValue != undefined && defaultValue != "") {
                $("#" + fileName + " option[value='" + defaultValue + "'] ").attr("selected", true);
            }
            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });
        }
    })
}

/**
 * 数据同步方法
 * **/
function SyncData(syncCode) {
    var index = layer.load(1, { shade: [0.7, '#393D49'] }, { shadeClose: true }); //0代表加载的风格，支持0-2
    $.ajax({
        url: "../System/SyncData",
        data: { syncCode: syncCode },
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (rsl.result) {
                layer.msg("数据同步程序执行完成");
            } else {
                layer.msg(data.message);
            }
            layer.close(index);
        }
    });
}


/**
仓库数据绑定
fileName：控件ID
**/
function WareHouseSelect(fileName, defaultValue) {
    
    $.ajax({
        url: "../Basic/GetDataList?dataType=wareHouse&page=1&limit=9999",
        dataType: "json",
        type: "GET",
        success: function (data) {
            var datas = data.data;
            $("select[name='" + fileName + "'] option:gt(0)").remove(); 
            for (var i = 0; i < datas.length; i++) {
                $("#" + fileName).append("<option value=\"" + datas[i]["houseCode"] + "\" >" + datas[i]["houseName"] + "(" + datas[i]["houseCode"] + ")" + "</option>");
            }
            if (defaultValue != undefined && defaultValue != "") {
                $("#" + fileName + " option[value='" + defaultValue + "'] ").attr("selected", true);
            }
            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });
        }
    });
}


/**
仓库货位数据绑定
fileName：控件ID
**/
function WarePositionSelect(fileName, wareHouse) {
    
    $.ajax({
        url: "../Basic/GetDataList?dataType=wp&page=1&limit=9999",
        data: { condition: wareHouse },
        dataType: "json",
        type: "GET",
        success: function (data) {
            var datas = data.data;
            $("select[name='" + fileName + "'] option:gt(0)").remove(); 
            if (datas != null) {
                for (var i = 0; i < datas.length; i++) {
                    $("#" + fileName).append("<option value=\"" + datas[i]["cCode"] + "\" >" + datas[i]["cName"] + "(" + datas[i]["cCode"] + ")" + "</option>");
                }
            } else {
                $("#" + fileName).append("<option value=\" \" > </option>");
            }
            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });
        }
    });
}


/**
 通用数据获取方法(下拉框)
type:通用数据类型
controlId:控件ID
defaultValue:默认值
 **/
function CommonData(type, controlId, defaultValue) {
    $.ajax({
        url: "../Basic/GetCommonData",
        data: { type: type },
        dataType: "json",
        type: "POST",
        async: false,
        success: function (rsl) {
            if (rsl.result) {
                var datas = rsl.data;
                $("select[name='" + controlId + "'] option:gt(0)").remove();   //先清空除第一个以外全部的option  ，再重新添加
                for (var i = 0; i < datas.length; i++) {
                    if (datas[i]["cCode"] != "") {
                        $("#" + controlId).append("<option value=\"" + datas[i]["cCode"] + "\" >" + datas[i]["cName"] + "(" + datas[i]["cCode"] + ")" + "</option>");
                    }

                }
                if (defaultValue != undefined && defaultValue != "") {
                    $("#" + controlId + " option[value='" + defaultValue + "'] ").attr("selected", true);
                }
                layui.use('form', function () {
                    var form = layui.form;
                    form.render();
                });
            }
        }
    });
}

/**
  * 通用数据树结构
  * dataType:数据类型
  * kjCode:树单击接收编码控件name
  * kjName:控件id(必须是label)
  * **/
function CommonTree(dataType,kjCode,kjName) {
    $.ajax({
        url: "../Basic/CommonDataTree",
        data: { dataType: dataType },
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (rsl.result) {
                loadTree(rsl.data, kjCode,kjName);
            } else {
                layer.msg(rsl.message);
            }
        }
    });
}

var typeCode = ''
var inventory = ''

function loadTree(data,kjCode,kjName) {
    var tree = layui.tree;
    var layer = layui.layer;
    var util = layui.util;
    //开启节点操作图标
    tree.render({
        elem: '#treeNav'
        , data: data
        , click: function (obj) {
            typeCode = obj.data["id"];
            inventory = obj.data["title"];
            $("input[ name='" + kjCode + "']").val(obj.data["id"]);
            $("#" + kjName).text("已选择：" + obj.data["title"]);
            loadData();
        }
    });
}

/**
 工序获取方法(下拉框)
controlId:控件ID
defaultValue:默认值
 **/
function getGxData( controlId, defaultValue) {
    $.ajax({
        url: "../Plan/getDataList?dataType=gxlist",
        //data: { type: type },
        dataType: "json",
        type: "GET",
        async: false,
        success: function (rsl) {
            //console.log(rsl)
            var datas = rsl.data;
            //console.log(datas)
            $("select[name='" + controlId + "'] option:gt(0)").remove();   //先清空除第一个以外全部的option  ，再重新添加

            for (var i = 0; i < datas.length; i++) {
                if (datas[i]["cCode"] != "") {
                    $("#" + controlId).append("<option value=\"" + datas[i]["cCode"] + "\" >" + datas[i]["cName"] + "</option>");  //+ "(" + datas[i]["cCode"] + ")"
                }

            }
            if (defaultValue != undefined && defaultValue != "") {
                $("#" + controlId + " option[value='" + defaultValue + "'] ").attr("selected", true);
            }
            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });
        }
    });
}
